package com.heima.schedule.controller;


import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dto.TaskDto;
import com.heima.model.schedule.entity.TaskInfo;
import com.heima.schedule.service.TaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    @PostMapping(path = "/add")
    public ResponseResult<Long> addTask(@RequestBody TaskDto task) {
        Long id = taskService.addTask(task);
        return ResponseResult.okResult(id);
    }

    /**
     * 取消任务
     *
     * @param taskId
     * @return
     */
    @DeleteMapping(path = "/{taskId}")
    public ResponseResult cancel(@PathVariable("taskId") Long taskId) {

        Boolean cancel = taskService.cancelTask(taskId);
        return ResponseResult.okResult(cancel);
    }

    /**
     * 消费任务
     *
     * @return
     */
    @GetMapping(path = "/poll/{taskType}/{priority}")
    public ResponseResult<TaskInfo> poll(@PathVariable("taskType") Integer taskType, @PathVariable("priority") Integer priority) {
        TaskInfo task = taskService.pollTask(taskType, priority);
        return ResponseResult.okResult(task);
    }

}
